package tk.mybatis.springboot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.springboot.dto.CommonResult;
import tk.mybatis.springboot.dto.QueryCondition;
import tk.mybatis.springboot.dto.QueryParam;
import tk.mybatis.springboot.service.BaseService;

@RestController
public abstract class BaseController <T > {

    @Autowired
    private BaseService <T> baseService;
    
    public abstract BaseService<T> getBaseService() ;
    @ResponseBody
    @RequestMapping(value = "/listpages")
    public Object listByPage(T t,
                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                       @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                       @RequestParam(name = "queryParams", required = false)  List <QueryParam> queryParams
                       ) {
        Example example= new QueryCondition().queryByExample(t, queryParams);
        List<T> list = baseService.getAllByPage(example ,pageNum,pageSize);
        return new CommonResult().pageSuccess(list);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(T t) {
         int count= baseService.save(t);
         if(count>0)
                 return new CommonResult().success(t);
        else

         return new CommonResult().failed();
    }
    @RequestMapping(value = "/batchSave", method = RequestMethod.POST)
    public Object batchSave(List<T> ts) {
         int count= baseService.batchSave(ts);
         if(count>0)
                 return new CommonResult().success(ts);
        else

         return new CommonResult().failed();
    }
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable Long id, @RequestBody T t) {
        int count = baseService.updateNotNull(t);
        if(count>0){
            return new CommonResult().success(count);
        }
        return new CommonResult().failed();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Object delete(@RequestParam("ids") List<Long> ids) {
        int count = baseService.delete(ids);
        if(count>0){
            return new CommonResult().success(count);
        }
        return new CommonResult().failed();
    }
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Object getItem(@PathVariable Long id) {
        T t = baseService.selectByKey(id);
        return new CommonResult().success(t);
    }

}

